export class Marker {
	constructor(viewer, iCesium) {
		this._viewer = viewer;
		this._iCesium = iCesium;
		this._dataSource = new iCesium.CustomDataSource('Marker-data');
		this._viewer.dataSources.add(this._dataSource);
		this.decorates = new Map();
	}

	/**
	 * 添加标签
	 * @returns {boolean}
	 */
	async add(markerData, callback) {
		const {
			id,
			coordinateType = 1,
			imagePath: image,
			textShow,
			textColor,
			textSize,
			textType = 'top',
			position,
			userData,
			...other
		} = markerData;
		if (this.decorates.get(id)) return console.error('decorator already registered');

		// 坐标位置
		if (+coordinateType) {
			if (Math.abs(position[0]) < Math.PI && Math.abs(position[1]) < Math.PI / 2) {
				markerData.position = this._iCesium.Cartesian3.fromRadians(...position);
			} else {
				markerData.position = this._iCesium.Cartesian3.fromDegrees(...position);
			}
		} else {
			markerData.position = new this._iCesium.Cartesian3(...position);
		}

		const color = (color) => color && this._iCesium.Color.fromCssColorString(color);
		const font = (textSize || 16) + 'px Helvetica'; // 字体
		const fillColor = color(textColor) || this._iCesium.Color.WHITE; // 标签颜色
		const scale = 1;
		const sizeInMeters = false;
		const pixelOffset =
			textType === 'Top'
				? new this._iCesium.Cartesian2(0, -(scale * 30))
				: new this._iCesium.Cartesian2(0, scale * 30);
		const disableDepthTestDistance = Number.POSITIVE_INFINITY; // 是否不被遮挡

		this._callback = callback || undefined; // 回调函数

		for (const key in other) {
			if (Object.prototype.hasOwnProperty.call(other, key)) {
				if (key.endsWith('Color') && !(key instanceof this._iCesium.Color)) {
					other[key] = color(other[key]);
				}
			}
		}
		const obj = {
			id: id,
			position: markerData.position,
			billboard: Object.assign(
				{
					image: image || pointImg,
					disableDepthTestDistance,
					scale,
					sizeInMeters
				},
				other
			),
			label: Object.assign(
				{
					// 文字标签
					show: textShow,
					font,
					scale,
					fillColor,
					pixelOffset, // 偏移量
					disableDepthTestDistance,
					sizeInMeters
				},
				other
			),
			userData
		};

		if (other.onlyText) delete obj.billboard;

		const entity = this._dataSource.entities.add(obj);

		this.decorates.set(id, { markerData, entity });

		callback && this.setHandler(callback);
	}

	/**
	 * 增加点击事件
	 * @param  {Function} callback 点击后的回调函数
	 */
	setHandler(callback) {
		const viewer = this._viewer;
		if (!viewer.markerHandler) {
			viewer.markerHandler = new this._iCesium.ScreenSpaceEventHandler(viewer.scene.canvas);
		}
		viewer.markerHandler.setInputAction((click) => {
			const pick = viewer.scene.pick(click.position);
			if (pick && typeof pick.id === 'object') {
				callback(this.decorates.get(pick.id.id.toString()));
			}
		}, this._iCesium.ScreenSpaceEventType.LEFT_CLICK);
	}

	/**
	 * 清空标签
	 * @returns {boolean}
	 */
	clear() {
		this._dataSource.entities.removeAll();
		this.decorates.clear();
		this._viewer.markerHandler = null;
		return true;
	}

	/**
	 * 删除标签
	 * @returns {boolean}
	 */
	delete(id) {
		this._dataSource.entities.removeById(id);
		this.decorates.delete(id);
		return true;
	}

	/**
	 * 获取标签
	 * @param {*} id
	 * @returns
	 */
	get(id) {
		return this._dataSource.entities.getById(id);
	}

	/**
	 * 更新标签
	 * @param id
	 * @param point
	 * @param markerData
	 * @returns {boolean}
	 */
	update(id, point, markerData) {}

	hide(ids) {}

	show(ids) {}

	hideAll() {}

	showAll() {}
}

export const MarkerData = [];

const pointImg =
	'';
